09 - Advanced vector and array, algorithm
09 - Advanced vector and array, algorithm
🔨 🔥 Assignment 🔥 🔨
Create a new project in the way explained in the first instruction.
Two dimensional vectors
There are multiple ways to create multidimensional array. One of them is to use nested std::vector
. To create a two-dimensional array (e.g. for storing the board in a checkers game) a vector of vectors can be used. Consider the example below:
const int number_of_rows = 5;
const int number_of_cols = 5;
std::vector<std::vector<int>> two_dimensional_vector;
for (int i = 0; i < number_of_rows; i++) {
std::vector<int> row;
for (int j = 0; j < number_of_cols; j++) {
10 * i + j);
row.emplace_back(
}
two_dimensional_vector.emplace_back(row);
}
std::cout << "Row 2, column 3: " << two_dimensional_vector[2][3] << std::endl;
🔨 🔥 Assignment 🔥 🔨
- Copy the code generating two dimensional array (using a
std::vector
ofstd::vector
s). Add a function that prints the 2d array separated by a tabulator (\t
) sign for columns and a new line for rows. The result should be:
0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
- Create a two dimensional array (using
std::vector
type) of size 3x3 and fill it with random values (0-10). Write a function that multiplies elements in the array by 2 (in place).
algorithm
module
The algorithm header file defines a set of utility functions designed mainly for collections. In order to use it include appropriate header:
#include <algorithm>
Interesting functions from algorithm
header:
- std::min_element - returns an iterator to smallest value in range
- std::max_element - returns an iterator to largest value in range
- std::fill - fills provided range of collection with value
- std::all_of - returns true if all the elements in range fulfil the provided condition, otherwise returns false
- std::any_of - returns true if any of the elements in range fulfil the provided condition, otherwise returns false
- std::none_of - returns true if none of the elements in range fulfil the provided condition, otherwise returns false
- std::find_if - returns iterator to the first element in range that fulfils the predicate
- std::count_if - returns the number of elements in range that fulfils the predicate
- std::sort - sorts the elements in the range
For more functions see: http://www.cplusplus.com/reference/algorithm/
🔨 🔥 Assignment 🔥 🔨
Create a structure
ExchangeRate
containing fields:date
-std:string
,rate
-float
.
Read the USD vs. PLN exchange rate data from file: USDvsPLN.csv. Store it in a
std::vector
ofExchangeRate
structures.Using
std::min_element
find a date when USD vs. PLN exchange rate was the smallest. Print the date and corresponding rate.Using
std::max_element
find a date when USD vs. PLN exchange rate was the largest. Print the date and corresponding rate.Check if all the exchange rates stored in file are from year 2020 (
std::all_of
).Find a date when the exchange rate went over 4.22 for the first time (
std::find_if
).Count for how many days the exchange rate was over 4.22 (
std::count_if
).
Final assignments 🔥 🔨
Exercise 1 - Tic-tac-toe game
Write a simple Tic-tac-toe game in the following steps:
create a variable that will let you store the game state (e.g.Â
std::vector<std::vector<char>>
)write a function that will print the current game state (board) in a readable way
write a function that will let a player insert
x
oro
in a specified location (ask the user for coordinates of the field)write a function that checks if one of the players has won
add game logic (turns) - use previously created functions, ask the players alternately to input their moves, print current board state, check for winner
Exercise 2
This year, due to a lack of a snow, Santa Claus forgot to prepare gifts and now asks you to help. Write a program, that will help him plan a flight. Santa will fly from north-west and would like to visit children in specific order. Read children’s names and coordinates of their houses (integer numbers, x rises in the east direction, y rises in the north direction) and print in an order form the one living the most north-west to the one living the most south-east. Use a proper structure and a sort function with a proper comparator.
Example input:
Kacper 1 1
Melchior -4 1
Baltazar -3 4
Maria 3 1
Example output:
Baltazar
Melchior
Kacper
Maria
Authors: Michał Fularz, Piotr Kaczmarek, Dominik Pieczyński, Tomasz Mańkowski, Jakub Tomczyński